<template>
  <div class="bg" v-show="$store.state.isLoginShow">
    <div class="divdiv" @click.stop="offLogin"></div>
    <div class="modal">
      <div class="title">
        登录
        <span @click="offLogin">X</span>
      </div>
      <div v-show="ifShow === true">
        <div class="box">
          <div class="box_l">
            <img src="@/assets/images/shouji.png" alt />
          </div>
          <div class="box_r">
            <div class="sm">
              <h3>扫码登录</h3>
              <img :src="qrimg" alt />
            </div>
            <p>
              使用
              <span>网易云音乐APP</span>扫码登录
            </p>
          </div>
        </div>
        <div class="btn" @click="qhlogin(false)">其他登录方式</div>
      </div>

      <div v-show="ifShow === false">
        <div class="login_left">
          <div class="phone_b">
            <img width="224" src="@/assets/images/computer.png" alt="" />
            <button @click="qhlogin(0)">手机号登录</button>
            <button>注册</button>
          </div>
          <div class="phone_rigth">
            <p>
              <i class="wx"></i>
              <span>微信登录</span>
            </p>
            <p>
              <i class="qq"></i>
              <span>QQ登录</span>
            </p>
            <p>
              <i class="wb"></i>
              <span>微博登录</span>
            </p>
            <p>
              <i class="youxiang"></i>
              <span>网易邮箱登录</span>
            </p>
          </div>
        </div>
        <div class="phone_botton">
          <input ref="input" type="checkbox" /><label>同意</label>
          <a href="javascript:;">《服务条款》</a>
          <a href="javascript:;">《隐私政策》</a>
          <a href="javascript:;">《儿童隐私政策》</a>
        </div>
        <div class="wximg">
          <img src="@/assets/images/wechat.png" @click="qhlogin(true)" />
        </div>
      </div>

      <div class="phone_login" v-show="ifShow === 0">
        <div class="phone_center">
          <div class="center_i phone_h">
            <div href="javascript:;">+86</div>
            <input type="text" placeholder="请输入手机号" v-model="phoneText" />
          </div>
          <div class="center_i phone_pass" v-show="passorcode">
            <input
              type="password"
              placeholder="请输入密码"
              v-model="phonepass"
            />
          </div>
          <div class="center_i phone_code">
            <input type="text" placeholder="请输入验证码" v-model="phoneCode" />
            <button @click="getCode" v-if="codeShow">获取验证码</button>
            <button v-else>{{ daojis }}</button>
          </div>
          <div class="center_i phone_pwd">
            <a href="javascript:;" @click="passAndcode">密码登录</a>
            <span>
              <input type="checkbox" />
              <p>自动登录</p>
            </span>
          </div>
          <button class="btn" @click="PhoneLogin">登录</button>
        </div>
        <div class="phone_bot">
          <span @click="qhlogin(true)"> 其它登录方式</span>
          <span>没有账号？免费注册</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  WeixinLogin,
  GetweixinLogin,
  CodeApi,
  PhoneLoginApi,
  ifPhoneApi,
  yanzheng,
  GetUresinfo,
} from "@/request/api";
const vaildatephone = (phoneText) => {
  let reg = /^1(3\d|4[5-9]|5[0-3,5-9]|6[2,5,6,7]|7[0-8]|8\d|9[0-3,5-9])\d{8}$/;
  return reg.test(phoneText.trim());
};
export default {
  data() {
    return {
      // uid
      uid: "",
      key: "",
      // 二维码图片
      qrimg: "",
      qrCheckData: {}, //状态,

      // 控制二维码手机号登录显示3选1
      ifShow: true,
      // 二维码
      qrimg: "",
      // 手机号
      phoneText: "16602812545",
      // 密码登录
      phonepass: "dengyibin001",
      // 验证码
      phoneCode: "5556",
      // 密码和验证码切换
      passorcode: false,
      // 点击了验证码的60秒倒计时
      daojis: 60,
      // 获取验证码v-if
      codeShow: true,
      // 要出入提示组件的数据
      tixingShow: {
        show: false,
        tixingText: "",
      },
      avatarUrl: "",
    };
  },

  created() {},
  methods: {
    passAndcode() {
      this.passorcode = !this.passorcode;
    },
    // 倒计时60秒组件
    daojiszj() {
      this.codeShow = false;
      let mms = setInterval(() => {
        this.daojis--;
      }, 1000);
      setTimeout(() => {
        clearInterval(mms);
        this.daojis = 60;
        this.codeShow = true;
      }, 60000);
    },
    // 关闭登录窗口
    offLogin() {
      this.$store.commit("ChanneLogin", false);
    },
    // 切换二维码和手机号登录
    qhlogin(val) {
      this.ifShow = val;
    },
    // 获取验证码
    getCode() {
      if (this.phoneText == "") {
        this.tixingShow.show = true;
        this.tixingShow.tixingText = "请输入电话号码";
        // 将提示组件打开
        this.$store.dispatch("Channetixingee", this.tixingShow);
        return;
      }
      let result = vaildatephone(this.phoneText);
      if (!result) {
        this.tixingShow.show = true;
        this.tixingShow.tixingText = "请输入正确的手机号";
        // 将提示组件打开
        this.$store.dispatch("Channetixingee", this.tixingShow);
        return;
      }
      CodeApi({ phone: this.phoneText }).then((res) => {
        if (res.data.code === 200) {
          console.log(res);
          this.tixingShow.show = true;
          this.tixingShow.tixingText = "验证码发送成功";
          // 将提示组件打开
          this.$store.dispatch("Channetixingee", this.tixingShow);
          this.daojiszj();
        } else {
          this.tixingShow.show = true;
          this.tixingShow.tixingText = res.data.message;
          // 将提示组件打开
          this.$store.dispatch("Channetixingee", this.tixingShow);
        }
      });
    },
    // 手机号密码登录
    PhoneLogin() {
      if (this.phoneText == "") {
        this.tixingShow.show = true;
        this.tixingShow.tixingText = "请输入电话号码";
        // 将提示组件打开
        this.$store.dispatch("Channetixingee", this.tixingShow);
        return;
      }
      let result = vaildatephone(this.phoneText);
      if (!result) {
        this.tixingShow.show = true;
        this.tixingShow.tixingText = "请输入正确的手机号";
        // 将提示组件打开
        this.$store.dispatch("Channetixingee", this.tixingShow);
        return;
      }
      if (!this.passorcode) {
        ifPhoneApi({ phone: this.phoneText }).then((res) => {
          if (res.data.code === 200) {
            // console.log(res);
            this.tixingShow.show = true;
            this.tixingShow.tixingText = `欢迎回来 ${res.data.nickname}`;
            // 将提示组件打开
            this.$store.dispatch("Channetixingee", this.tixingShow);
            // 用户头像
            this.$store.commit("ChanneavatarUrl", res.data.avatarUrl);
            // 关闭登录框
            this.$store.commit("ChanneLogin", false);
            // 修改header登录和头像显示
            this.$store.commit("ChanneisGoLogin", false);
            // GetUresinfo(uid)

            // GetUresinfo({ uid: this.uid }).then((res) => {
            //   console.log(res.data);
            // });
          } else if (res.data.exist === -1) {
            this.tixingShow.show = true;
            this.tixingShow.tixingText = "您还没有注册";
            // 将提示组件打开
            this.$store.dispatch("Channetixingee", this.tixingShow);
          }
        });
      } else {
        PhoneLoginApi({
          phone: this.phoneText,
          password: this.phonepass,
          captcha: this.phoneCode,
        }).then((res) => {
          console.log(res);
          // 关闭登录框
          this.$store.commit("ChanneLogin", false);
          // 修改header登录和头像显示
          this.$store.commit("ChanneisGoLogin", false);
        });
      }
    },
    //获取key
    getKey() {
      WeixinLogin({}).then((res) => {
        // console.log(res);
        this.key = res.data.data.unikey;
      });
      this.loginQqr(this.key);
    },
    // 获取二维码图片
    loginQqr(key) {
      GetweixinLogin({
        key: key,
        timerstamp: new Date().getTime(),
      }).then((res) => {
        // console.log(res.data);
        this.qrimg = res.data.data.qrimg;
      });
    },
    // 判断801 还是扫码成功
    qrCheck() {
      yanzheng({
        key: this.key,
        timerstamp: new Date().getTime(), //传入参数时间戳
        withCredentials: true,
      }).then((res) => {
        console.log(res);
        if (res.data.code === 803) {
          this.qrCheckData = res.data;
          sessionStorage.setItem("cookie", res.data.cookie);
        }
      });
    },
    getStatus() {
      dlzhuangtai(sessionStorage.getItem("cookie")).then((res) => {
        sessionStorage.getItem("cookie");
        // console.log(res.data.data.account.id,res.data.data.account.status);
        // console.log(res.data.data.profile.avatarUrl,res.data.data.profile.nickname);

        sessionStorage.setItem("isLogin", res.data.data.account.status);
        sessionStorage.setItem("userid", res.data.data.account.id);
        sessionStorage.setItem("avatarUrl", res.data.data.profile.avatarUrl);
        sessionStorage.setItem("nickname", res.data.data.profile.nickname);
        console.log(res);

        // console.log( sessionStorage.getItem('isLogin'));
        // console.log(sessionStorage.getItem("nickname"));
      });
    },
  },
  watch: {
    isLogin() {
      if (this.$store.state.isGoLoginShow) {
        this.qrCheck();
      }
    },
  },
  mounted() {
    this.getKey();
  },
};
</script>
 
<style lang = "less" scoped>
@import "@/assets/base.less";

.bg {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  background-color: rgba(0, 0, 0, 0.5);
  .divdiv {
    width: 100%;
    height: 100%;
  }
  .modal {
    background-color: #fff;
    width: 530px;
    position: absolute;
    left: 0;
    top: 25%;
    right: 0;
    margin: auto;
    height: 400px;

    .title {
      width: 100%;
      height: 40px;
      font-size: 14px;
      padding: 0px 20px;
      box-sizing: border-box;
      line-height: 40px;
      color: #fff;
      background-color: #000;
      font-weight: bold;

      span {
        float: right;
        font-weight: 400;
      }
    }

    .box {
      margin: 20px 0;
      display: flex;
      justify-content: center;
      align-items: center;

      .box_l {
        img {
          width: 125px;
          height: 220px;
        }
      }

      .box_r {
        p {
          margin-left: 55px;
          text-align: center;
          font-size: 12px;
          line-height: 17px;
          color: rgba(0, 0, 0, 0.4);

          span {
            cursor: pointer;
            color: #0c73c2;
          }
        }

        .sm {
          position: relative;
          margin-left: 60px;

          span {
            position: absolute;
            left: 0;
            top: 0;
          }

          h3 {
            font-size: 18px;
            color: #333;
            width: 150px;
            text-align: center;
          }
        }
      }
    }
    .btn {
      display: block;
      width: 118px;
      height: 28px;
      margin: 20px auto 0;
      padding-right: 0;
      font-size: 12px;
      border: 1px solid #979797;
      border-radius: 15px;
      line-height: 28px;
      text-align: center;
      color: rgba(0, 0, 0, 0.8);
      margin-bottom: 20px;
      cursor: pointer;
    }
    .login_left {
      display: flex;
      align-items: center;
      justify-content: center;
      margin: 30px 0;
      position: relative;
      .phone_b {
        border-right: 1px solid #ccc;
        padding-right: 30px;
        img {
          display: block;
        }
        button {
          display: block;
          width: 219px;
          height: 30px;
          margin-top: 15px;
          border: 1px solid #ccc;
          cursor: pointer;
          &:first-of-type {
            background: #0c73c2;
            color: #fff;
            &:hover {
              background: #027ad6;
            }
          }
          &:last-of-type {
            &:hover {
              background: rgb(255, 255, 255);
            }
          }
        }
      }
      .phone_rigth {
        color: #333;
        padding-left: 30px;
        p {
          height: 50px;
          display: flex;
          align-items: center;
          i {
            width: 38px;
            height: 38px;
            margin-right: 14px;
            font-size: 30px;
            margin-right: 10px;
            cursor: pointer;
          }
          .wx {
            background: url(@/assets/images/login.png) no-repeat;
            background-position: -150px -670px;
          }
          .qq {
            background: url(@/assets/images/login.png) no-repeat;
            background-position: -190px -670px;
          }
          .wb {
            background: url(@/assets/images/login.png) no-repeat;
            background-position: -231px -670px;
          }
          .youxiang {
            background: url(@/assets/images/login.png) no-repeat;
            background-position: -271px -670px;
          }
          span {
            font-size: 12px;
            color: #666;
            cursor: pointer;
            &:hover {
              text-decoration: underline;
            }
          }
        }
      }
    }
    .phone_botton {
      height: 20px;
      font-size: 12px;
      color: #999;
      display: flex;
      align-items: center;
      margin-top: 10px;
      input {
        margin: 0 5px 0 50px;
      }
      a {
        color: #5185c0;
        text-decoration: none;
      }
    }
    .wximg {
      width: 52px;
      height: 52px;
      position: absolute;
      bottom: 1px;
      right: 1px;
      img {
        width: 52px;
      }
    }
    .phone_login {
      font-size: 12px;
      .phone_center {
        /* height: 270px; */
        width: 250px;
        margin: auto;
        overflow: hidden;
        input {
          border: 0;
          outline: none;
          font-size: 12px;
        }
        a {
          text-decoration: none;
        }
        .phone_h {
          margin-top: 40px;
          border: 1px solid #ccc;
          display: flex;
          align-items: center;
          border-radius: 20px;
          div {
            width: 40px;
            height: 40px;
            padding-right: 5px;
            line-height: 40px;
            text-align: center;
            color: #333;
            position: relative;
            border-right: 1px solid #ccc;
          }
          input {
            flex: 1;
            height: 100%;
            text-indent: 1em;
            border-radius: 20px;
          }
        }
        .center_i {
          height: 40px;
          margin-bottom: 10px;
        }
        .phone_pass {
          input {
            height: 40px;
            width: 99%;
            text-indent: 1em;
            border: 1px solid #ccc;
            border-radius: 20px;
          }
        }
        .phone_code {
          display: flex;
          justify-content: space-between;
          input {
            width: 180px;
            text-indent: 1em;
            border: 1px solid #ccc;
            border-radius: 20px;
          }
          button {
            width: 60px;

            border-radius: 20px;
            background-color: @red;
            border: none;
            color: #fff;
            font-size: 12px;
          }
        }
        .phone_pwd {
          display: flex;
          justify-content: space-between;
          align-items: center;
          a {
            color: #666;
            &:hover {
              text-decoration: underline;
            }
          }
          span {
            display: inline-block;
            height: 40px;
            display: flex;
            align-items: center;
            input {
              margin-right: 5px;
            }
          }
        }
        button {
          cursor: pointer;
        }
        .btn {
          width: 100%;
          height: 40px;
          background: @red;
          color: #fff;
          border: 1px solid #ccc;
          border-radius: 20px;
        }
      }
      .phone_bot {
        height: 50px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-top: 1px solid #666;
        span {
          cursor: pointer;
          &:first-of-type {
            margin-left: 20px;
            color: #0c72c3;
          }
          &:last-of-type {
            margin-right: 20px;
            color: #999;
          }
        }
      }
    }
  }
}
</style>